package com.hubujun.driverlicense.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hubujun.driverlicense.DLCore;
import com.hubujun.driverlicense.R;
import com.hubujun.driverlicense.model.QuestionModel;
import com.hubujun.driverlicense.util.Ln;
import com.hubujun.driverlicense.util.TextUtils;
import com.hubujun.driverlicense.util.ViewUtils;
import com.hubujun.driverlicense.view.IconFontTextView;
import com.hubujun.driverlicense.view.ScratchTextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.List;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment {

    TextView textView;
    ImageView imageView;
    IconFontTextView answerATextView;
    IconFontTextView answerBTextView;
    IconFontTextView answerCTextView;
    IconFontTextView answerDTextView;
    ScratchTextView scratchTextView;
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        Ln.d("PlaceholderFragment newInstance sectionNumber=%s", sectionNumber);
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        initView(rootView);
        renderView();

        return rootView;
    }

    private void initView(View rootView) {
        textView = (TextView) rootView.findViewById(R.id.question_content);
        imageView = (ImageView) rootView.findViewById(R.id.question_url);
        answerATextView = (IconFontTextView) rootView.findViewById(R.id.answer_a);
        answerBTextView = (IconFontTextView) rootView.findViewById(R.id.answer_b);
        answerCTextView = (IconFontTextView) rootView.findViewById(R.id.answer_c);
        answerDTextView = (IconFontTextView) rootView.findViewById(R.id.answer_d);
        scratchTextView = (ScratchTextView) rootView.findViewById(R.id.explains);

        scratchTextView.setRevealListener(new ScratchTextView.IRevealListener() {
            @Override
            public void onRevealed(ScratchTextView tv) {
                // on reveal
            }
        });
    }

    private void renderView() {
        int index = getArguments().getInt(ARG_SECTION_NUMBER);
        List<QuestionModel> questionModelList = DLCore.getInstance().getAccountStorage().getQuestionStorage().getQuestionList();
        final QuestionModel questionModel = questionModelList.get(index);
        if (questionModel == null) {
            return;
        }
        textView.setText(index + "." + questionModel.question);
        if (!TextUtils.isNullOrEmpty(questionModel.url)) {
            ImageLoader.getInstance().displayImage(questionModel.url, imageView, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    Ln.d("renderView onLoadingStarted url=%s", questionModel.url);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    Ln.d("renderView onLoadingComplete width=%s,height=%s", loadedImage.getWidth(), loadedImage.getHeight());
                    int width = 0;
                    int height = ViewUtils.dipToPx(getActivity(), 200);
                    int screenWidth = ViewUtils.getDisplayWidth(getActivity());
                    if (loadedImage.getWidth() < 300) {
                        width = 150;
                    } else {
                        width = 600;
                    }
                    float f = width / loadedImage.getWidth();
                    height = (int) (loadedImage.getHeight() * (f));
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewUtils.dipToPx(getActivity(), width), ViewUtils.dipToPx(getActivity(), height));
                    imageView.setLayoutParams(params);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {

                }
            });
        }
        answerATextView.setText(questionModel.item1);
        answerBTextView.setText(questionModel.item2);
        answerCTextView.setText(questionModel.item3);
        answerDTextView.setText(questionModel.item4);
        scratchTextView.setText(questionModel.explains);
    }
}
